# *-*- coding:utf-8 -*-
# @Author  : Eliza
# @Time    : 2021/7/18 21:37
# @Software : RubyMine

# DateTime<Date
# Time
# *Both three classes include Comparable modules
require 'date'
require 'time' #可以使用更多方法功能
puts "----设置时间：string -> time"
timeStr = "2021/11/2"
puts Date.parse(timeStr) #2021-11-02
puts Date.parse('03/3/2000') #2000-03-03
puts Date.strptime('03/23/2000', '%m/%d/%y') #2000-03-23
puts Date.strptime('03-23-2000', '%m-%d-%y') #2000-03-23
TRY_FORMATS = ['%d-%m-%y', '%y-%m-%d']

def tryToParse(timeS)
  parsed = nil
  TRY_FORMATS.each do |type|
    begin
      parsed = Date.strptime(timeS, type)
      break
    rescue ArgumentError
    end
  end
  parsed
end

puts "-------计算时间差---"
start_date = Date.new(2015, 3, 2)
end_date = Date.new(2015, 3, 9)
(start_date..end_date).each { |x| puts x } #中间时间
puts "----"
start_date.upto(end_date) { |x| puts x }
puts "----"

start_date.step(end_date, 2) { |x| puts x } #step=2

puts "-------TimeZone---"
puts Time.now #2021-07-18 22:13:34 +0800
puts Time.now.isdst #false（系统时间）
puts Time.now.gmtime #2021-07-18 14:15:10 UTC
puts Time.now.localtime #2021-07-18 22:15:10 +0800
puts DateTime.now.new_offset #2021-07-18T14:16:13+00:00 格林威治 标准时间
puts DateTime.now.new_offset(Rational(8, 24)) #2021-07-18T22:17:08+08:00 8/24 24时区 东+ 8区
puts "-----改变RUBY环境时区-----"
puts Time.now.isdst #false

ENV['TZ'] = 'America/Los_Angeles'
puts Time.now.isdst #true
puts "-----special format"
# Time.iso8601( '2001-04-17T19:23:17.201Z')
# Time.rfc822('Tue,1 Jul 200310:52:37 +0200')
# Time.httpdate( 'Tue,05 Sep 2006 16:05:51 GMT ')
#print date and time
puts Time.now.iso8601 #2021-07-18T22:08:37+08:00

puts Time.now.strftime('%m-%d-%y') #07-18-21，根据字符串形式输出

puts "------Basic----------"
puts Time.now
puts Time.class #Class
puts Date.today.class #Date
puts DateTime.now.class #DateTime
puts Date.superclass #Object
puts Time.superclass #Object
# Date
date0 = Date.today
puts date0
puts date0.year, date0.day, date0.month, date0.wday #周日 0
puts date0.prev_day, date0.next_day, date0.next_day.wday
puts date0 + 1 #天数加一
puts "-------------"
#Time
time0 = Time.now
puts time0 #2021-07-18 21:51:44 +0800
puts time0.year, time0.day, time0.month, time0.wday #周日 0
puts time0.hour, time0.min, time0.sec
puts time0 + 1 #2021-07-18 21:51:45 +0800 秒数加一 ，最小分度值
puts time0.zone #中国标准时间 时区
